package mashibing.class1;

/**
 * 二分法查看有序数组数字存不存在
 *
 * @author yujiafan
 * @version 1.0.0
 * @create 2023-03-04
 */
public class Class1_4_BinaryNumExist {

    public static int queryBinaryNum(int[] arr, int num) {
        if (arr == null || arr.length == 0) {
            return -1;
        }
        int L = 0;
        int R = arr.length - 1;
        int mid = arr.length / 2;
        while (L <= R) {
            if (arr[mid] > num) {
                R = mid - 1;
            } else if (arr[mid] < num) {
                L = mid + 1;
            } else {
                return mid;
            }
            mid = L + ((R - L) >> 1);
        }
        return -1;
    }

    public static void main(String[] args) {
        int[] arr = {1, 3, 6, 8, 9, 10, 11};
        System.out.println(queryBinaryNum(arr, 11));
    }
}
